package xiaoa.java.push.queue;

import java.util.LinkedHashMap;

import xiaoa.java.log.L;
import xiaoa.java.push.bean.EmailMessageBean;
import xiaoa.java.push.push.PushEmail;



/**
 * 邮件推送队列
 * @author lkc
 * @date 2017年6月22日 下午3:39:46
 * @version V1.0
 *
 */
public class SendEmailQueue extends QueueBase<EmailMessageBean>{
    
    // email 发送器
    private static PushEmail  push;
    
    private static SendEmailQueue  queue;
    
    public static void init(int coreThread , int maxThread ,String emailSendHost , String emailSendUsername , String emailSendPassword ){
        
        if (emailSendHost == null || emailSendUsername == null || emailSendPassword == null
                || emailSendHost.isEmpty() || emailSendUsername.isEmpty() || emailSendPassword.isEmpty()
                ){
            throw new RuntimeException("参数有误");
        }
        
        queue =   new SendEmailQueue();
        queue.init(coreThread, maxThread);
        push  =   new PushEmail(emailSendHost, emailSendUsername, emailSendPassword);
        
    }
    
    /**
     * 推送消息
     * @Title: doPushMessage
     * @param bean
     * @param tid
     * @author lkc
     */
    public static void doPushMessage(final EmailMessageBean  bean){
        queue.pushMessage(bean, L.getTransactionIdThreadLocal());
    }


    @Override
    protected void doPushMessage(EmailMessageBean bean,LinkedHashMap<String, Object> logMap)throws Throwable {

        logMap.put("process", "start");
        logMap.put("email", bean.getTo());
        logMap.put("size", bean.getText() == null ? 0 : bean.getText().length() );
        
        // 发送邮件
        push.sendEmail(bean);
        
        logMap.put("process", "succ");
    }
    
    
    public static int queueSize() {
        
        // TODO 自动生成方法存根
        return queue.size();
        
    }
    
    

}
